﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class TurretBuild : MonoBehaviour {

    public TurretData turret1;
    public TurretData turret3;
    public TurretData turretSelected;
    private int money=1010;
    public Text moneyText;
    public GameObject gainMoney;
    public Animator moneyLess;
    private Transform buildPointPosition;
    public GameObject TipText;
    public Canvas canvas;
    private Coroutine coroutine;
    private void Update()
    {
            if (Input.GetMouseButtonDown(0))
            {
                if (EventSystem.current.IsPointerOverGameObject() == false)
                {
                    RaycastHit2D hit;
                    hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward, 1000f, LayerMask.GetMask("BuildPoint"));
                    if (hit.collider != null)
                    {
                        BuildPoint buildPoint = hit.collider.GetComponent<BuildPoint>();

                        if (money <= 0)
                        { return; }

                        if (buildPoint.turret == null && money >= turretSelected.cost)
                        {
                            if (turretSelected.turret != null)
                            {
                                Debug.Log("canbuild");
                                ChangeMoney(turretSelected.cost);
                                buildPoint.TurretBuild(turretSelected.turret);
                            }
                            else
                            {
                            //提示要选择
                            ShowTip("请先选择炮车");
                            Invoke("HideTip", 2);
                            }
                            
                        }
                        else if (buildPoint.turret == null && money < turretSelected.cost)
                        {
                            //提示钱不够
                            moneyLess.SetTrigger("moneyless");

                        }
                        else if (buildPoint.turret != null)
                        {
                           // buildPointPosition.position = buildPoint.turret.transform.position;
                            //提示已有炮台
                            
                            ShowTip("已有炮台请换位置");
                            Invoke("HideTip", 2);
                        }


                    }
                }
            }
        
    }

    private void ShowTip(string text)
    {
        TipText.SetActive(true);
        TipText.GetComponentInChildren<Text>().text = text;
    }

    private void HideTip()
    {
        TipText.SetActive(false);
    }
    public void ShowGainMoney(int count,Vector3 pos)
    {
        gainMoney.transform.position=WorldToUI(canvas, pos);
        //gainMoney.transform.position = pos;
        gainMoney.SetActive(true);
        gainMoney.GetComponentInChildren<Text>().text = "+"+count;
        gainMoney.GetComponent<MoneyMove>().Move();
    }

    public static Vector3 WorldToUI(Canvas cvs, Vector3 point)
    {
        Vector3 pt = Camera.main.WorldToScreenPoint(point);

        Vector3 ff = cvs.worldCamera.ScreenToWorldPoint(pt);
        Vector3 v_new = new Vector3(ff.x, ff.y, cvs.GetComponent<RectTransform>().anchoredPosition3D.z);
        
        return v_new;
    }

    public void HideGainMoney()
    {
        gainMoney.SetActive(false);
    }

    public void ChangeMoney(int change)
    {
        money -= change;
        moneyText.text = "￥" + money;
    }

    public void OnTurret1Selected(bool isOn)
    {
        if(isOn)
        {
            
            turretSelected = turret1;
        }
    }
    public void OnTurret3Selected(bool isOn)
    {
        if (isOn)
        {
            turretSelected = turret3;
        }
    }
}
